package com.hengpeng.itfinoss.controller.repayment;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.common.util.MoneyUtil;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.constants.RespCodeConstants.CommonCode;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.idAssign.ID;
import com.hengpeng.itfinbase.message.partner.PartnerTransType;
import com.hengpeng.itfinbase.message.partner.QueryBalanceRequest;
import com.hengpeng.itfinbase.message.partner.QueryBalanceResponse;
import com.hengpeng.itfinbase.message.partner.RepaymentRequestMsg;
import com.hengpeng.itfinbase.message.partner.RepaymentResponseMsg;
import com.hengpeng.itfinbase.persist.BorrowInfo;
import com.hengpeng.itfinbase.persist.BorrowInfo.BorrowStatus;
import com.hengpeng.itfinbase.persist.InvestInfo;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.ReceivePlan.ReceiveStatus;
import com.hengpeng.itfinbase.persist.RecoverRepay;
import com.hengpeng.itfinbase.persist.RecoverRepay.RecoverRepayStatus;
import com.hengpeng.itfinbase.persist.RepaymentPlan;
import com.hengpeng.itfinoss.constant.MSConstants;
import com.hengpeng.itfinoss.constant.SystemConfigUtil;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.repayment.vo.RecoverRepayVo;
import com.hengpeng.itfinoss.controller.repayment.vo.RepaymentPlanVo;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.util.BeanClone;
import com.hengpeng.itfinoss.util.JsonUtils;

/**
 * <pre>
 * 还款管理控制器
 * </pre>
 * @author weiyuan
 * @version 1.0, 2015年5月27日
 */
@Controller
@RequestMapping("/repayment")
public class RepaymentController extends BaseController
{
	@Autowired(required = false)
	protected ServiceFactory serviceFactory;

	/**
	 * <pre>
	 * 标的还款列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @param borrowInfoName 借款标的名称
	 * @return
	 */
	@RequestMapping(value = "/borrowInfoRepayMentList")
	public String borrowInfoRepayMentList(ModelMap map, HttpServletRequest request, String status, String beginDate,
			String endDate, String borrowUserName, String borrowInfoName)
	{
		List<RepaymentPlanVo> listVo = new ArrayList<RepaymentPlanVo>();
		Pagination page = new Pagination(request);
		if (StringHelper.isEmpty(status))
		{// 默认显示待还款计划信息
			status = RepaymentPlan.ReplaymentStatus.UNSETTLE.toString();
		}
		ServiceResult<List<RepaymentPlan>> serviceResult = serviceFactory.getRepaymentService().getRepaymentPlanList(
				page.currentPage, page.pageSize, status, borrowUserName, borrowInfoName, beginDate, endDate);
		page.setEntityCount(serviceResult.getReturnListSize());
		List<RepaymentPlan> repaymentPlans = serviceResult.getReturnValue();
		for (RepaymentPlan rp : repaymentPlans)
		{
			RepaymentPlanVo repaymentPlanVo = BeanClone.clone(rp, RepaymentPlanVo.class);
			repaymentPlanVo.setTotalMoney(MoneyUtil.convertFenToYuan(rp.getRepayMoney() + rp.getRepayAddMoney()));
			repaymentPlanVo.setRepayAddMoneyStr(MoneyUtil.convertFenToYuan(rp.getRepayAddMoney()));
			repaymentPlanVo.setRepayBaseMoneyStr(MoneyUtil.convertFenToYuan(rp.getRepayBaseMoney()));
			repaymentPlanVo.setRepayInterestMoneyStr(MoneyUtil.convertFenToYuan(rp.getRepayInterestMoney()));
			repaymentPlanVo.setOverduePenaltyStr(MoneyUtil.convertFenToYuan(rp.getOverduePenalty()));
			repaymentPlanVo.setPeriods(rp.getCurRepayNum() + "/" + rp.getTotalRepayNum());
			if (rp.getStatus() == RepaymentPlan.ReplaymentStatus.SETTLING)
			{// 还款中
				if (rp.getType() == RepaymentPlan.ReplaymentType.ADVANCE)
				{// 平台垫付
					repaymentPlanVo.setPlatRepaymentUrl("<a onclick=repaymentByPlat('" + rp.getBorrowInfoId() + "','"
							+ rp.getId() + "') style='cursor: pointer;'>垫付</a>");// 平台垫付操作
				}
				else if (rp.getType() == RepaymentPlan.ReplaymentType.NORMAL)
				{// 借款人正常还款
					repaymentPlanVo.setRepaymentUrl("<a onclick=repayment('" + rp.getBorrowInfoId() + "','"
							+ rp.getId() + "') style='cursor: pointer;'>还款</a>");// 标的还款操作
				}
			}
			else if (rp.getStatus() == RepaymentPlan.ReplaymentStatus.UNSETTLE)
			{// 待还款
				repaymentPlanVo.setRepaymentUrl("<a onclick=repayment('" + rp.getBorrowInfoId() + "','" + rp.getId()
						+ "') style='cursor: pointer;'>还款</a>");// 标的还款操作

				repaymentPlanVo.setPlatRepaymentUrl("<a onclick=repaymentByPlat('" + rp.getBorrowInfoId() + "','"
						+ rp.getId() + "') style='cursor: pointer;'>垫付</a>");// 平台垫付操作
			}
			else if (rp.getStatus() == RepaymentPlan.ReplaymentStatus.REPAYMENTED)
			{// 已还清
				if (rp.getType() == RepaymentPlan.ReplaymentType.ADVANCE)
				{// 平台垫付
					repaymentPlanVo.setPlatRepaymentUrl("已还清");
				}
				else if (rp.getType() == RepaymentPlan.ReplaymentType.NORMAL)
				{// 借款人正常还款
					repaymentPlanVo.setRepaymentUrl("已还清");
				}
			}
			repaymentPlanVo.setBorrowUserNameUrl("<a onclick=userInfoDetail('" + rp.getBorrowUserId()
					+ "') style='cursor: pointer;'>" + rp.getBorrowUserName() + "</a>");
			repaymentPlanVo.setDetailUrl("<a onclick=repaymentDetail('" + rp.getBorrowInfoId() + "','"
					+ rp.getCurRepayNum() + "')  style='cursor: pointer;'>详细</a>");
			listVo.add(repaymentPlanVo);
		}
		map.put("listVo", listVo);
		map.put("page", page);
		map.put("status", status);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		map.put("borrowUserName", borrowUserName);
		map.put("borrowInfoName", borrowInfoName);
		return "/repayment/borrowInfoRepayMentList";
	}

	/**
	 * <pre>
	 * 查看还款明细
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/repaymentDetail")
	public String repaymentDetail(ModelMap map, HttpServletRequest request, String borrowinfoId, String curRepayNum)
	{
		List<ReceivePlan> receivePlans = serviceFactory.getRepaymentService().getReceivePlans(
				Long.parseLong(borrowinfoId), Integer.parseInt(curRepayNum), null);
		map.put("listVo", receivePlans);
		return "/repayment/repaymentDetail";
	}

	/**
	 * <pre>
	 * 标的还款操作
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/doBorrowInfoRepayMent")
	public String doBorrowInfoRepayMent(ModelMap map, HttpServletRequest request, String borrowinfoId,
			String repaymentPlanId)
	{
		/**
		 * <pre>
		 * 思路步骤: 
		 * 1,调用QueryBalanceBg接口查询余额是否够用. 
		 * 2,获取标的信息,还款计划信息,本期还款的明细信息 
		 * 3,根据投资人数量生成还款计划批次号,最多499个人为一个批次,并将该批次号更新至收款计划表
		 * 4,根据还款明细信息,生成还款明细json格式字符串信息
		 * 5,更新还款计划表状态为：还款中
		 * 
		 * 注意事项：
		 * 还款接口里的(还款金额+还款手续费)<= 投标金额*(1 + Borrowerrate（投标时设置的))
		 * </pre>
		 */
		BorrowInfo borrowInfo = serviceFactory.getBorrowInfoService().getBorrowInfo(Long.parseLong(borrowinfoId),BorrowStatus.REPAYMENTING);
		RepaymentPlan repaymentPlan = serviceFactory.getRepaymentService().getUnRepaymentPlan(Long.parseLong(repaymentPlanId));
		if (repaymentPlan == null) {
			return returnError(map, "该还款计划信息不存在,或还款计划状态异常!");
		}
		List<ReceivePlan> receivePlans = serviceFactory.getRepaymentService().getReceivePlans(
				Long.parseLong(borrowinfoId), repaymentPlan.getCurRepayNum(),
				new ReceiveStatus[] { ReceiveStatus.WAITREC, ReceiveStatus.RECEIVING });
		if (borrowInfo == null) {
			return returnError(map, "标的信息不存在,或标的状态异常,请联系平台人员");
		}
		if (receivePlans == null || receivePlans.size() < 1) {
			return returnError(map, "无还款明细信息,或还款明细信息异常!");
		}
		/**
		 * 判断出账账户号余额是否够用
		 */
		Long totalRepaymentMoney = 0l;
		for (ReceivePlan p : receivePlans) {
			totalRepaymentMoney += p.getRecMoney();
		}
		QueryBalanceRequest balanceRequest = new QueryBalanceRequest();
		balanceRequest.setUserId(borrowInfo.getBorrowUserId());
		balanceRequest.setUsrCustId(borrowInfo.getBorrowUserTrustAccountId());
		balanceRequest.setPartnerTransType(PartnerTransType.QueryBalanceBg);
		balanceRequest.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
		QueryBalanceResponse balanceResponse = (QueryBalanceResponse) serviceFactory.getRemoteService().remoteTrans(balanceRequest);
		if (CommonCode.TRANS_SUC.equals(balanceResponse.getRespCode())) {
			if (totalRepaymentMoney > (Double.parseDouble(balanceResponse.getAvlBal().replaceAll(",", "")) * 100)) {
				return returnError(map, "余额不足!");
			}
		} else {
			return returnError(map, balanceResponse.getRespDesc());
		}
		/**
		 * 批量发送还款请求
		 */
		batchPostRepaymentRequest(receivePlans.size(), 0, receivePlans, borrowinfoId, repaymentPlanId,
				borrowInfo.getBorrowUserTrustAccountId(), false, null);
		return returnSuccess(map, "/repayment/borrowInfoRepayMentList");
	}

	/**
	 * <pre>
	 * 批量发送还款请求递归方法
	 * </pre>
	 * @param size
	 * @param begin
	 * @param list
	 * @param borrowinfoId
	 * @param repaymentPlanId
	 * @param outCustId 出账账户
	 * @param isPlatfor 标识是否平台垫付
	 * @param dzBorrCustId 当为垫资还款此参数必款，商户指明为哪个用户垫资
	 */
	private void batchPostRepaymentRequest(int size, int begin, List<ReceivePlan> list, String borrowinfoId,
			String repaymentPlanId, String outCustId, boolean isPlatfor, String dzBorrCustId)
	{
		Integer batch_repayment_num = Integer
				.parseInt(SystemConfigUtil.getConfigValue(MSConstants.BATCH_REPAYMENT_NUM));
		if (size > batch_repayment_num)
		{
			postRepaymentRequest(begin, begin + batch_repayment_num, list, borrowinfoId, repaymentPlanId, outCustId,
					isPlatfor, dzBorrCustId);
			list = list.subList(begin + batch_repayment_num, list.size());
			batchPostRepaymentRequest(list.size(), 0, list, borrowinfoId, repaymentPlanId, outCustId, isPlatfor,
					dzBorrCustId);
		}
		else
		{
			postRepaymentRequest(begin, list.size(), list, borrowinfoId, repaymentPlanId, outCustId, isPlatfor,
					dzBorrCustId);
		}
	}

	/**
	 * <pre>
	 * 批量发送还款请求执行方法
	 * </pre>
	 * @param begin
	 * @param end
	 * @param list
	 * @param borrowinfoId
	 * @param repaymentPlanId
	 * @param outCustId
	 * @param isPlatfor
	 */
	private void postRepaymentRequest(int begin, int end, List<ReceivePlan> list, String borrowinfoId,
			String repaymentPlanId, String outCustId, boolean isPlatfor, String dzBorrCustId)
	{
		RepaymentRequestMsg reqMsg = new RepaymentRequestMsg();
		String batchId = ID.getInstanse().getID(18).toString();// 生成批次号

		Long transAmt = 0l;// 应还款总额
		/**
		 * 生成还款明细json格式字符串
		 */
		List<ReceivePlan> postList = list.subList(begin, end);
		Map<String, List<Map<String, String>>> inDetailsMap = new HashMap<String, List<Map<String, String>>>();
		List<Map<String, String>> custidsList = new ArrayList<Map<String, String>>();
		if (postList != null && postList.size() > 0)
		{
			for (ReceivePlan receivePlan : postList)
			{
				InvestInfo investInfo = serviceFactory.getPersistService().get(InvestInfo.class,
						receivePlan.getInvestmentInfoId());
				Map<String, String> custidsMap = new HashMap<String, String>();
				custidsMap.put("InCustId", investInfo.getNowOwnUserTrustAccountId());// 入账账户,取投标计划的当前债权人
				custidsMap.put("OrdId", String.valueOf(receivePlan.getId()));// 收款计划id
				custidsMap.put("SubOrdId", receivePlan.getInvestmentInfoId().toString());// 投标记录id
				custidsMap.put("TransAmt", MoneyUtil.convertFenToYuan(receivePlan.getRecMoney()));// 还款金额
				if (isPlatfor)
				{// 是否垫付
					custidsMap.put("DzBorrCustId", dzBorrCustId);// 垫资还款交易
				}
				custidsList.add(custidsMap);
				transAmt += receivePlan.getRecMoney();
				reqMsg.getReceivePlanIds().add(receivePlan.getId());
			}
		}
		inDetailsMap.put("InDetails", custidsList);
		/**
		 * 发送还款请求
		 */
		reqMsg.setBorrowinfoId(Long.parseLong(borrowinfoId));
		reqMsg.setRepaymentPlanId(repaymentPlanId);
		reqMsg.setOutCustId(outCustId);// 出账账户号
		reqMsg.setBatchId(batchId);// 批次号
		reqMsg.setMerOrdDate(DateTimeUtil.formatToStr(new Date(), DateTimeUtil.DATE_FORMAT_YYYYMMDD));
		reqMsg.setProId(borrowinfoId);// 借款标的id
		reqMsg.setInDetails(JsonUtils.createGson().create().toJson(inDetailsMap));// 还款明细
		reqMsg.setTransAmt(MoneyUtil.convertFenToYuan(transAmt));// 还款总额
		reqMsg.setPlatfor(isPlatfor);// 标识是否平台垫付
		reqMsg.setFee(MSConstants.CHAR_ZERO);// 手续费默认为0
		reqMsg.setPartnerTransType(PartnerTransType.BatchRepayment);
		reqMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
		serviceFactory.getRemoteService().remoteTrans(reqMsg);// 批量还款不接收及时返回信息
	}

	/**
	 * <pre>
	 * 标的还款平台垫付操作
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/doBorrowInfoRepayMentByPlat")
	public String doBorrowInfoRepayMentByPlat(ModelMap map, HttpServletRequest request, String borrowinfoId,
			String repaymentPlanId)
	{
		/**
		 * <pre>
		 * step: 
		 * 1,判断余额是否足够 
		 * 2,获取标的信息,还款计划信息,本期还款的明细信息 
		 * 3,获取担保账户托管账户id 
		 * 4,根据投资人数量生成还款计划批次号,最多499个人为一个批次,并将该批次号更新至收款计划表
		 * 5,根据还款明细信息,生成还款明细json格式字符串信息 
		 * 6,更新还款计划表状态为：还款中
		 * </pre>
		 */
		BorrowInfo borrowInfo = serviceFactory.getBorrowInfoService().getBorrowInfo(Long.parseLong(borrowinfoId),
				BorrowStatus.REPAYMENTING);
		RepaymentPlan repaymentPlan = serviceFactory.getRepaymentService().getUnRepaymentPlan(
				Long.parseLong(repaymentPlanId));
		if (repaymentPlan == null)
		{
			return returnError(map, "该还款计划信息不存在,或还款计划状态异常!");
		}
		List<ReceivePlan> receivePlans = serviceFactory.getRepaymentService().getReceivePlans(
				Long.parseLong(borrowinfoId), repaymentPlan.getCurRepayNum(),
				new ReceiveStatus[] { ReceiveStatus.WAITREC, ReceiveStatus.RECEIVING });
		if (borrowInfo == null)
		{
			return returnError(map, "标的信息不存在,或标的状态异常,请联系平台人员");
		}
		if (receivePlans == null || receivePlans.size() < 1)
		{
			return returnError(map, "无还款明细信息,或还款明细信息异常!");
		}
		/**
		 * 判断出账账户号余额是否够用
		 */
		Long totalRepaymentMoney = 0l;
		for (ReceivePlan p : receivePlans)
		{
			totalRepaymentMoney += p.getRecMoney();
		}
		QueryBalanceRequest balanceRequest = new QueryBalanceRequest();
		balanceRequest.setUserId(borrowInfo.getGuaranteeUserId());
		balanceRequest.setUsrCustId(borrowInfo.getGuaranteeUserTrustAccountId());
		balanceRequest.setPartnerTransType(PartnerTransType.QueryBalanceBg);
		balanceRequest.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
		QueryBalanceResponse balanceResponse = (QueryBalanceResponse) serviceFactory.getRemoteService().remoteTrans(
				balanceRequest);
		if (CommonCode.TRANS_SUC.equals(balanceResponse.getRespCode()))
		{
			if (totalRepaymentMoney > (Double.parseDouble(balanceResponse.getAvlBal().replaceAll(",", "")) * 100))
			{
				return returnError(map, "余额不足!");
			}
		}
		else
		{
			return returnError(map, balanceResponse.getRespDesc());
		}
		/**
		 * 批量发送还款请求
		 */
		batchPostRepaymentRequest(receivePlans.size(), 0, receivePlans, borrowinfoId, repaymentPlanId,
				borrowInfo.getGuaranteeUserTrustAccountId(), true, borrowInfo.getBorrowUserTrustAccountId());
		return returnSuccess(map, "/repayment/borrowInfoRepayMentList");
	}

	/**
	 * <pre>
	 * 追偿信息列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 追偿状态
	 * @param 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/recoverRepayMentList")
	public String recoverRepayMentList(ModelMap map, HttpServletRequest request,
			RecoverRepay.RecoverRepayStatus status, String borrowUserName)
	{
		List<RecoverRepayVo> listVo = new ArrayList<RecoverRepayVo>();
		Pagination page = new Pagination(request);
		if (status == null)
		{// 默认显示未追回的垫付
			status = RecoverRepay.RecoverRepayStatus.UNRECOVER;
		}
		ServiceResult<List<RecoverRepay>> serviceResult = serviceFactory.getRepaymentService().getRecoverRepayList(
				page.currentPage, page.pageSize, status, borrowUserName);
		page.setEntityCount(serviceResult.getReturnListSize());

		List<RecoverRepay> recoverRepays = serviceResult.getReturnValue();
		for (RecoverRepay rp : recoverRepays)
		{
			RecoverRepayVo recoverRepayVo = BeanClone.clone(rp, RecoverRepayVo.class);
			if (rp.getStatus() == RecoverRepay.RecoverRepayStatus.RECOVERED)
			{
				recoverRepayVo.setPaybackUrl("已追回");
			}
			else if (rp.getStatus() == RecoverRepay.RecoverRepayStatus.RECOVERING)
			{
				recoverRepayVo.setPaybackUrl("偿还中");
			}
			else
			{
				recoverRepayVo.setPaybackUrl("<a onclick=payback('"
						+ (rp.getBorrowInfoId() + "','" + rp.getRepaymentPlanId()) + "','" + rp.getId()
						+ "') style='cursor: pointer;'>偿还垫资</a>");// 偿还垫资操作
			}
			recoverRepayVo.setBorrowUserNameUrl("<a onclick=userInfoDetail('" + rp.getBorrowUserId()
					+ "') style='cursor: pointer;'>" + rp.getBorrowUserName() + "</a>");
			listVo.add(recoverRepayVo);
		}
		map.put("listVo", listVo);
		map.put("page", page);
		map.put("status", status);
		map.put("borrowUserName", borrowUserName);
		return "/repayment/recoverRepayMentList";
	}

	/**
	 * <pre>
	 * 偿还垫资操作
	 * </pre>
	 * @param map
	 * @param request
	 * @param recoverRepayId 追偿信息id
	 * @param borrowinfoId 借款标的id
	 * @param repaymentPlanId 还款计划id
	 * @return
	 */
	@RequestMapping(value = "/doRecoverRepayMent")
	public String doRecoverRepayMent(ModelMap map, HttpServletRequest request, HttpServletResponse response,
			String recoverRepayId, String borrowinfoId, String repaymentPlanId) throws Exception
	{
		/**
		 * <pre>
		 * step: 
		 * 1,判断余额是否够用 
		 * 2,获取借款标的信息,获取追偿信息 
		 * 3,拼装请求参数发送请求
		 * 4,还垫资交易处理即时返回信息,也要做回调接口
		 * </pre>
		 */
		BorrowInfo borrowInfo = serviceFactory.getBorrowInfoService().getBorrowInfo(Long.parseLong(borrowinfoId), null);
		RecoverRepay recoverRepay = serviceFactory.getRepaymentService().getRecoverRepay(Long.parseLong(recoverRepayId),new RecoverRepayStatus[] { RecoverRepayStatus.UNRECOVER, RecoverRepayStatus.RECOVERING });
		if (borrowInfo == null) {
			return returnError(map, "标的信息不存在,或标的状态异常,请联系平台人员");
		}
		if (recoverRepay == null) {
			return returnError(map, "追偿信息不存在或状态异常");
		}
		/**
		 * 判断出账账户号余额是否够用
		 */
		QueryBalanceRequest balanceRequest = new QueryBalanceRequest();
		balanceRequest.setUserId(borrowInfo.getBorrowUserId());
		balanceRequest.setUsrCustId(borrowInfo.getBorrowUserTrustAccountId());
		balanceRequest.setPartnerTransType(PartnerTransType.QueryBalanceBg);
		balanceRequest.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
		QueryBalanceResponse balanceResponse = (QueryBalanceResponse) serviceFactory.getRemoteService().remoteTrans(balanceRequest);
		if (CommonCode.TRANS_SUC.equals(balanceResponse.getRespCode())) {
			if (recoverRepay.getRecoverRepayMoney() > (Double.parseDouble(balanceResponse.getAvlBal().replaceAll(",","")) * 100)) {
				return returnError(map, "余额不足!");
			}
		} else {
			return returnError(map, balanceResponse.getRespDesc());
		}

		RepaymentRequestMsg reqMsg = new RepaymentRequestMsg();
		reqMsg.setOrdId(recoverRepayId);// 订单编号追偿信息id
		reqMsg.setOutCustId(borrowInfo.getBorrowUserTrustAccountId());// 出账账户号,即为借款人托管账户
		reqMsg.setInCustId(borrowInfo.getGuaranteeUserTrustAccountId());// 入账账户,即为担保人托管账户
		reqMsg.setGuaranteeUserId(borrowInfo.getGuaranteeUserId());// 担保人用户id
		reqMsg.setBorrowinfoId(Long.parseLong(borrowinfoId));// 借款标的id
		reqMsg.setTransAmt(MoneyUtil.convertFenToYuan(recoverRepay.getRecoverRepayMoney()));// 还款总额
		reqMsg.setFee("0.00");// 手续费默认为0
		reqMsg.setPartnerTransType(PartnerTransType.Repayment);
		reqMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
		reqMsg.setMerPriv("recoverRepayment");// 添加偿还垫资标识
		RepaymentResponseMsg repaymentResponseMsg = (RepaymentResponseMsg) serviceFactory.getRemoteService().remoteTrans(reqMsg);
		if ("000".equals(repaymentResponseMsg.getRespCode())) {
			response.getWriter().print(repaymentResponseMsg.getReturnNotifyParamValue());
			return returnSuccess(map, "/repayment/recoverRepayMentList");
		} else {
			return returnError(map, repaymentResponseMsg.getRespDesc());
		}
	}
}
